package org.dragonnova.meetingapp.app.adapter;

import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.dragonnova.meetingapp.R;
import org.dragonnova.meetingapp.model.bean.VoiceInfo;
import org.dragonnova.meetingapp.utils.TimeUtil;

import java.util.ArrayList;

/**
 * Created by hucn on 2016/5/11.
 * Description:聊天页面的聊天item的适配器
 */

public class ChatListAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<VoiceInfo> chatList;
    private String name = Build.MODEL;

    public ChatListAdapter(Context context, ArrayList<VoiceInfo> chatList) {
        this.chatList = chatList;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return chatList.size();
    }

    @Override
    public Object getItem(int position) {
        return chatList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_record, null);
            holder = new Holder();
            holder.record_tv_time = (TextView) convertView.findViewById(R.id.record_tv_time);

            holder.record_rl_other = (RelativeLayout) convertView.findViewById(R.id.record_rl_other);

            holder.record_tv_other_recognized = (TextView) convertView.findViewById(R.id.record_tv_other_recognized);
            holder.record_tv_other_translated = (TextView) convertView.findViewById(R.id.record_tv_other_translated);
            holder.record_tv_other_speaker = (TextView) convertView.findViewById(R.id.record_tv_other_speaker);

            holder.record_rl_me = (RelativeLayout) convertView.findViewById(R.id.record_rl_me);

            holder.record_tv_me_recognized = (TextView) convertView.findViewById(R.id.record_tv_me_recognized);
            holder.record_tv_me_translated = (TextView) convertView.findViewById(R.id.record_tv_me_translated);
            holder.record_tv_me_speaker = (TextView) convertView.findViewById(R.id.record_tv_me_speaker);

            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        VoiceInfo voiceItem = chatList.get(position);

        holder.record_tv_time.setText(TimeUtil.geTime(voiceItem.getInsertTime()));

        if (!name.equals(voiceItem.getUserId())) {

            holder.record_rl_other.setVisibility(View.VISIBLE);
            holder.record_rl_me.setVisibility(View.GONE);

            if (voiceItem.getOrgContent() != null)
                holder.record_tv_other_recognized.setText(voiceItem.getOrgContent());
            if (voiceItem.getTranContent() != null)
                holder.record_tv_other_translated.setText(voiceItem.getTranContent());
            if (voiceItem.getUserId() != null)
                holder.record_tv_other_speaker.setText(voiceItem.getUserId());
        } else {

            holder.record_rl_other.setVisibility(View.GONE);
            holder.record_rl_me.setVisibility(View.VISIBLE);

            if (voiceItem.getOrgContent() != null)
                holder.record_tv_me_recognized.setText(voiceItem.getOrgContent());
            if (voiceItem.getTranContent() != null)
                holder.record_tv_me_translated.setText(voiceItem.getTranContent());
            if (voiceItem.getUserId() != null)
                holder.record_tv_me_speaker.setText(voiceItem.getUserId());
        }
        return convertView;
    }

    public static class Holder {
        private TextView record_tv_time;
        private RelativeLayout record_rl_other;
        private TextView record_tv_other_recognized;
        private TextView record_tv_other_translated;
        private TextView record_tv_other_speaker;

        private RelativeLayout record_rl_me;
        private TextView record_tv_me_recognized;
        private TextView record_tv_me_translated;
        private TextView record_tv_me_speaker;

    }
}